package mcheli.weapon;

import mcheli.MCH_Lib;
import mcheli.aircraft.MCH_AircraftInfo;
import mcheli.tank.MCH_EntityTank;
import mcheli.wrapper.W_Entity;
import net.minecraft.entity.Entity;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

public class MCH_WeaponAAMissile extends MCH_WeaponEntitySeeker {

    public MCH_WeaponAAMissile(World w, Vec3 v, float yaw, float pitch, String nm, MCH_WeaponInfo wi) {
        super(w, v, yaw, pitch, nm, wi);
        super.power = 12;
        super.acceleration = 2.5F;
        super.explosionPower = 4;
        super.interval = 5;
        if (w.isRemote) {
            super.interval += 5;
        }

        super.guidanceSystem.canLockInAir = true;
        super.guidanceSystem.ridableOnly = wi.ridableOnly;
    }

    public boolean isCooldownCountReloadTime() {
        return true;
    }

    public void update(int countWait) {
        super.update(countWait);
    }

    @Override
    public boolean shot(MCH_WeaponParam prm) {
        boolean result = false;
        if (!super.worldObj.isRemote) {
            if (getInfo().passiveRadar || getInfo().activeRadar) {
                this.playSound(prm.entity);

                float yaw, pitch;
                if (getInfo().enableOffAxis) {
                    yaw = prm.user.rotationYaw + super.fixRotationYaw;
                    pitch = prm.user.rotationPitch + super.fixRotationPitch;
                } else {
                    yaw = prm.entity.rotationYaw + super.fixRotationYaw;
                    pitch = prm.entity.rotationPitch + super.fixRotationPitch;
                }
                if (prm.entity instanceof MCH_EntityTank) {
                    MCH_EntityTank tank = (MCH_EntityTank) prm.entity;
                    yaw = prm.user.rotationYaw;
                    pitch = prm.user.rotationPitch;
                    yaw += prm.randYaw;
                    pitch += prm.randPitch;
                    int wid = tank.getCurrentWeaponID(prm.user);
                    MCH_AircraftInfo.Weapon w = tank.getAcInfo().getWeaponById(wid);
                    float minPitch = w == null ? tank.getAcInfo().minRotationPitch : w.minPitch;
                    float maxPitch = w == null ? tank.getAcInfo().maxRotationPitch : w.maxPitch;
                    float playerYaw = MathHelper.wrapAngleTo180_float(tank.getRotYaw() - yaw);
                    float playerPitch = tank.getRotPitch() * MathHelper.cos((float) (playerYaw * Math.PI / 180.0D))
                        + -tank.getRotRoll() * MathHelper.sin((float) (playerYaw * Math.PI / 180.0D));
                    float playerYawRel = MathHelper.wrapAngleTo180_float(yaw - tank.getRotYaw());
                    float yawLimit = (w == null ? 360F : w.maxYaw);
                    float relativeYaw = MCH_Lib.RNG(playerYawRel, -yawLimit, yawLimit);
                    yaw = MathHelper.wrapAngleTo180_float(tank.getRotYaw() + relativeYaw);
                    pitch = MCH_Lib.RNG(pitch, playerPitch + minPitch, playerPitch + maxPitch);
                    pitch = MCH_Lib.RNG(pitch, -90.0F, 90.0F);
                }
                double tX = -MathHelper.sin(yaw / 180.0F * 3.1415927F) * MathHelper.cos(pitch / 180.0F * 3.1415927F);
                double tZ = MathHelper.cos(yaw / 180.0F * 3.1415927F) * MathHelper.cos(pitch / 180.0F * 3.1415927F);
                double tY = -MathHelper.sin(pitch / 180.0F * 3.1415927F);
                MCH_EntityAAMissile e = new MCH_EntityAAMissile(super.worldObj, prm.posX, prm.posY, prm.posZ, tX, tY, tZ, yaw, pitch, (double) super.acceleration);
                if (yaw > 180.0F) {//so we are just basically defining yaw to like not go 360 mlg mode right hopefully pray to god it works okay
                    yaw -= 360.0F;
                } else if (yaw < -180.0F) {
                    yaw += 360.0F;
                }
                e.setName(super.name);
                e.setParameterFromWeapon(this, prm.entity, prm.user);
                super.worldObj.spawnEntityInWorld(e);
                result = true;
            } else {
                Entity tgtEnt = prm.user.worldObj.getEntityByID(prm.option1);
                if (tgtEnt != null && !tgtEnt.isDead) {
                    this.playSound(prm.entity);
                    float yaw, pitch;
                    if (getInfo().enableOffAxis) {
                        yaw = prm.user.rotationYaw + super.fixRotationYaw;
                        pitch = prm.user.rotationPitch + super.fixRotationPitch;
                    } else {
                        yaw = prm.entity.rotationYaw + super.fixRotationYaw;
                        pitch = prm.entity.rotationPitch + super.fixRotationPitch;
                    }
                    if (prm.entity instanceof MCH_EntityTank) {
                        MCH_EntityTank tank = (MCH_EntityTank) prm.entity;
                        yaw = prm.user.rotationYaw;
                        pitch = prm.user.rotationPitch;
                        yaw += prm.randYaw;
                        pitch += prm.randPitch;
                        float minPitch = tank.getSeatInfo(prm.entity) == null ? tank.getAcInfo().minRotationPitch : tank.getSeatInfo(prm.entity).minPitch;
                        float maxPitch = tank.getSeatInfo(prm.entity) == null ? tank.getAcInfo().maxRotationPitch : tank.getSeatInfo(prm.entity).maxPitch;
                        float playerYaw = MathHelper.wrapAngleTo180_float(tank.getRotYaw() - yaw);
                        float playerPitch = tank.getRotPitch() * MathHelper.cos((float) (playerYaw * Math.PI / 180.0D))
                            + -tank.getRotRoll() * MathHelper.sin((float) (playerYaw * Math.PI / 180.0D));
                        pitch = MCH_Lib.RNG(pitch, playerPitch + minPitch, playerPitch + maxPitch);
                        pitch = MCH_Lib.RNG(pitch, -90.0F, 90.0F);
                    }
                    double tX = -MathHelper.sin(yaw / 180.0F * 3.1415927F) * MathHelper.cos(pitch / 180.0F * 3.1415927F);
                    double tZ = MathHelper.cos(yaw / 180.0F * 3.1415927F) * MathHelper.cos(pitch / 180.0F * 3.1415927F);
                    double tY = -MathHelper.sin(pitch / 180.0F * 3.1415927F);
                    MCH_EntityAAMissile e = new MCH_EntityAAMissile(super.worldObj, prm.posX, prm.posY, prm.posZ, tX, tY, tZ, yaw, pitch, (double) super.acceleration);
                    if (yaw > 180.0F) {//so we are just basically defining yaw to like not go 360 mlg mode right hopefully pray to god it works okay
                        yaw -= 360.0F;
                    } else if (yaw < -180.0F) {
                        yaw += 360.0F;
                    }
                    e.setName(super.name);
                    e.setParameterFromWeapon(this, prm.entity, prm.user);
                    e.setTargetEntity(tgtEnt);
                    super.worldObj.spawnEntityInWorld(e);
                    result = true;
                }
            }
        } else {
            if (getInfo().passiveRadar || getInfo().activeRadar) {
                result = true;
            } else if (super.guidanceSystem.lock(prm.user) && super.guidanceSystem.lastLockEntity != null) {
                result = true;
                super.optionParameter1 = W_Entity.getEntityId(super.guidanceSystem.lastLockEntity);
            }
        }

        return result;
    }

    @Override
    public boolean lock(MCH_WeaponParam prm) {
        if (!super.worldObj.isRemote) {
            // do nothing
        } else {
            if (getInfo().passiveRadar) {
                if (!super.guidanceSystem.lock(prm.user)) {
//               if(getInfo().enableBVR && tick % 5 == 0) {
//                  Map<Integer, MCH_EntityInfo> map = MCH_RenderBVRLockBox.currentLockedEntities;
//                  if(!map.isEmpty()) {
//                     MCH_EntityInfo info = (MCH_EntityInfo) map.values().toArray()[rand.nextInt(map.size())];
//                     if(info != null) {
//                        MCH_EntityAAMissile.clientTrackingMissile.forEach((K, V) -> {
//                           int mslId = K;
//                           if (V.getInfo() != null && V.getInfo().passiveRadar && V.getInfo().enableBVR) {
//                              MCH_MOD.getPacketHandler().sendToServer(new PacketLockTargetBVR(
//                                      mslId, (int) info.posX, (int) info.posY, (int) info.posZ
//                              ));
//                           }
//                        });
//                     }
//                  }
//               }
                }
                if (guidanceSystem.isLockComplete()) {
                    Entity target = guidanceSystem.lastLockEntity;
                    //获取玩家射击的AA弹
                    for (MCH_EntityBaseBullet bullet : getShootBullets(worldObj, prm.user, getInfo().maxLockOnRange)) {
                        bullet.clientSetTargetEntity(target);
                        super.optionParameter1 = W_Entity.getEntityId(target);
                    }
                } else {
                    for (MCH_EntityBaseBullet bullet : getShootBullets(worldObj, prm.user, getInfo().maxLockOnRange)) {
                        bullet.clientSetTargetEntity(null);
                        super.optionParameter1 = 0;
                    }
                }


            }
        }
        return false;
    }

    @Override
    public void onUnlock(MCH_WeaponParam prm) {
        if (worldObj.isRemote) {
            if (guidanceSystem != null && prm.user != null) {
                if (!guidanceSystem.isLockComplete()) {
                    for (MCH_EntityBaseBullet bullet : getShootBullets(worldObj, prm.user, getInfo().maxLockOnRange)) {
                        bullet.clientSetTargetEntity(null);
                        super.optionParameter1 = 0;
                    }
                }
            }
//         if(getInfo().enableBVR) {
//            if(tick % 10 == 0) {
//               MCH_EntityAAMissile.clientTrackingMissile.forEach((K, V) -> {
//                  int mslId = K;
//                  if (V.getInfo() != null && V.getInfo().passiveRadar && V.getInfo().enableBVR) {
//                     MCH_MOD.getPacketHandler().sendToServer(new PacketLockTargetBVR(mslId, 0, -1, 0));
//                  }
//               });
//            }
//         }
        }
    }
}
